<template>
  <Select ref="select" v-model="selectValue" :filterable="filterable" :clearable="clearable"
          :multiple="multiple"
          @on-change="onChange">
    <Option v-for="item in selectList" :value="item.code" :key="item.code" :label="item.name">{{ item.name }}</Option>
  </Select>
</template>

<script>
import {getDict, getEnableDict} from "@/util";

export default {
  name: "DictSelect",
  props: ['dictCode', 'modelValue', "isShowDisableDict", 'filterable', 'clearable', 'multiple'],
  expose: ['reload'],
  watch: {
    modelValue(newVal, oldVal) {
      this.selectValue = newVal
    }
  },
  data() {
    return {
      dictDatas: [],
      selectList: [],
      selectValue: undefined,
    }
  },
  created() {
    this.reload()
  },
  methods: {
    reload() {
      if (!this.dictCode || this.dictCode == '') {
        return
      }
      if (this.isShowDisableDict) {
        getDict(this.dictCode).then(res => {
          this.dictDatas = res.datas
          this.selectList = this.dictDatas
          this.selectValue = this.modelValue
        })
      } else {
        getEnableDict(this.dictCode).then(res => {
          this.dictDatas = res.datas
          this.selectList = this.dictDatas
          this.selectValue = this.modelValue
        })
      }
    },
    onChange(v) {
      this.selectValue = v;
      this.$emit("update:modelValue", v)
      this.$emit("on-change", v)
    }
  }
}
</script>

<style scoped>

</style>
